package br.edu.sistemaacademico;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.DefaultComboBoxModel;
import javax.swing.GroupLayout;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

import br.edu.sistemaacademico.helper.Conexao;
import br.edu.sistemaacademico.pojo.Aluno;
import br.edu.sistemaacademico.pojo.Professor;
import br.edu.sistemaacademico.pojo.Servidor;
import br.edu.sistemaacademico.proxy.LoginProxy;
import client.ClientCommand;

public class FrmLogin extends JFrame{
	private javax.swing.JButton jButton1;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPasswordField jPasswordField1;
    private javax.swing.JTextField jTextField1; 
    private Aluno aluno=null;
    private Professor professor=null;
    private Servidor servidor=null;
    LoginProxy login = new LoginProxy();
    FrmModelo frmC;
    
	public FrmLogin() {
	        initComponents();
	        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
	        this.addWindowListener(new WindowAdapter() {
	        	public void windowClose(WindowEvent e){
	        		Conexao.fecharConexao();
	        		System.exit(0);
	        		
	        	}
			});
	    }

	    @SuppressWarnings("unchecked")
	    private void initComponents() {
	        jLabel1 = new javax.swing.JLabel();
	        jTextField1 = new javax.swing.JTextField();
	        jComboBox1 = new javax.swing.JComboBox();
	        jLabel2 = new javax.swing.JLabel();
	        jLabel3 = new javax.swing.JLabel();
	        jLabel4 = new javax.swing.JLabel();
	        jPasswordField1 = new javax.swing.JPasswordField();
	        jButton1 = new javax.swing.JButton();

	        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
	        setName("Form"); // NOI18N

	        jLabel1.setFont(Font.getFont("Verdana-Plain-18"));
	        jLabel1.setText("Logar"); 
	        jLabel1.setName("jLabel1");

	        jTextField1.setText(""); // NOI18N
	        jTextField1.setName("jTextField1"); // NOI18N

	        jComboBox1.setModel(new DefaultComboBoxModel(new String[] { "Professor", "Servidor", "Aluno"}));
	        jComboBox1.setName("jComboBox1"); // NOI18N

	        jLabel2.setText("Tipo de Usuario" ); // NOI18N
	        jLabel2.setName("jLabel2"); // NOI18N

	        jLabel3.setText("Usuario:"); // NOI18N
	        jLabel3.setName("jLabel3"); // NOI18N

	        jLabel4.setText("Senha:"); // NOI18N
	        jLabel4.setName("jLabel4"); // NOI18N

	        jPasswordField1.setText(""); // NOI18N
	        jPasswordField1.setName("jPasswordField1"); // NOI18N

	        jButton1.setText("Efetar Login"); // NOI18N
	        jButton1.setName("jButton1"); // NOI18N
	        jButton1.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent e) {
					if(jTextField1.getText().equals("")||jPasswordField1.getText().equals("")){
						new JOptionPane().showMessageDialog(null, "Preencha todos os Campos");
					}else{
						if(jComboBox1.getSelectedItem().equals("Professor")){
								//Professor professor = (Professor) ClientCommand.carregaObjeto("ProfessorService","login = '" + jTextField1.getText() + "' and senha = '"+jPasswordField1.getText()+"'" , "");
								professor = (Professor)login.logar("Professor", jTextField1.getText(), jPasswordField1.getText());
								if(professor!=null&&professor.getSenha().equals(jPasswordField1.getText())	&& professor.getLogin().equals(jTextField1.getText())){
								setVisible(false);
								frmC = new frmProfessor(new JFrame(), true,professor);
								setVisible(true);
								}else
								new JOptionPane().showMessageDialog(null, "Usuario ou Senha invalidos");
						}
						if(jComboBox1.getSelectedItem().equals("Aluno")){
								aluno = (Aluno) login.logar("Aluno", jTextField1.getText(), jPasswordField1.getText());
								if(aluno!=null&&aluno.getSenha().equals(jTextField1.getText()) &&aluno.getSenha().equals(jPasswordField1.getText())){
									setVisible(false);
								    frmC  = new frmAluno(new JFrame(), true, aluno);
								    setVisible(true);
								}else
								new JOptionPane().showMessageDialog(null, "Usuario ou Senha invalidos");							
						}
						if(jComboBox1.getSelectedItem().equals("Servidor")){
								//servidor = (Servidor)ClientCommand.carregaObjeto("ServidorService","login = '" + jTextField1.getText() + "' and senha = '"+jPasswordField1.getText()+"'" , "");
								servidor = (Servidor)login.logar("Servidor", jTextField1.getText(), jPasswordField1.getText());
								if(servidor!=null&&servidor.getSenha().equals(jPasswordField1.getText())	&& servidor.getLogin().equals(jTextField1.getText())){
								setVisible(false);
								frmC = new frmServidor(new JFrame(), true,servidor);
								setVisible(true);
								}else
								new JOptionPane().showMessageDialog(null, "Usuario ou Senha invalidos");				
						}
					}
					
				}
			});
	      
	        GroupLayout layout = new GroupLayout(getContentPane());
	        getContentPane().setLayout(layout);
	        layout.setHorizontalGroup(
	            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
	            .addGroup(layout.createSequentialGroup()
	                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
	                    .addGroup(layout.createSequentialGroup()
	                        .addGap(33, 33, 33)
	                        .addGroup(layout.createParallelGroup(GroupLayout.Alignment.TRAILING)
	                            .addComponent(jLabel3)
	                            .addComponent(jLabel2)
	                            .addComponent(jLabel4))
	                        .addGap(18, 18, 18)
	                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
	                            .addComponent(jPasswordField1)
	                            .addComponent(jTextField1, javax.swing.GroupLayout.Alignment.TRAILING)
	                            .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE)))
	                    .addGroup(layout.createSequentialGroup()
	                        .addGap(82, 82, 82)
	                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE))
	                    .addGroup(layout.createSequentialGroup()
	                        .addGap(103, 103, 103)
	                        .addComponent(jLabel1)))
	                .addContainerGap(24, Short.MAX_VALUE))
	        );
	        layout.setVerticalGroup(
	            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	            .addGroup(layout.createSequentialGroup()
	                .addGap(6, 6, 6)
	                .addComponent(jLabel1)
	                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
	                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
	                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
	                    .addComponent(jLabel2))
	                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
	                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
	                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
	                    .addComponent(jLabel3))
	                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
	                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
	                    .addComponent(jPasswordField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
	                    .addComponent(jLabel4))
	                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
	                .addComponent(jButton1)
	                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
	        );

	        pack();
	    }// </editor-fold>

	    
	    public static void main(String args[]) {
	        java.awt.EventQueue.invokeLater(new Runnable() {	
	            public void run() {
	                new FrmLogin().setVisible(true);
	            }
	        });
	    }
	   

    }
